using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Windows.Forms;

namespace asWins.Forms
{
	/// <summary>
	/// Class asTabForm for showing tab of child form.
	/// </summary>
	public class asTabForm
	{
		#region Constructor

		/// <summary>
		/// Initialize a new tab form.
		/// </summary>
		/// <param name="form"></param>
		/// <param name="formId"></param>
		/// <param name="active"></param>
		/// <param name="parent"></param>
		public asTabForm(BaseForm form, int formId, bool active, asMdiManager parent)
		{
			this.m_OwnerForm = null;
			this.m_FormId = -1;

			this.m_IsActive = active;
			this.m_OwnerForm = form;
			this.m_FormId = formId;
			this.Text = form.Text;
			this.Size = this.MeasureString(this.Text);
			this.m_Parent = parent;
			this.Init();
		}

		/// <summary>
		/// Set default.
		/// </summary>
		private void Init()
		{
			this.Height = this.m_Parent.TabHeight;
			this.BackColor = this.m_Parent.TabColor;
		}

		#endregion

		#region Variables

		private const int PADDING_SIZE = 10;

		private Point m_Location;
		/// <summary>
		/// Property Location (Point)
		/// </summary>
		public Point Location
		{
			get {return m_Location;}
			set {m_Location = value;}
		}

		private Size m_Size;
		/// <summary>
		/// Property Size (Size)
		/// </summary>
		public Size Size
		{
			get {return m_Size;}
			set {m_Size = value;}
		}

		/// <summary>
		/// Property Height (int)
		/// </summary>
		public int Height
		{
			get {return this.m_Size.Height;}
			set {this.m_Size.Height = value;}
		}

		/// <summary>
		/// Property Width (int)
		/// </summary>
		public int Width
		{
			get {return this.m_Size.Width;}
			set {this.m_Size.Width = value;}
		}

		private string m_Text;
		/// <summary>
		/// Property Text (string)
		/// </summary>
		public string Text
		{
			get {return m_Text;}
			set {m_Text = value;}
		}

		/// <summary>
		/// Get Text and FormId.
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return (this.Text + "[" + this.FormId.ToString() + "]");
		}


		private Color m_BackColor;
		/// <summary>
		/// Property BackColor (Color)
		/// </summary>
		public Color BackColor
		{
			get {return m_BackColor;}
			set {m_BackColor = value;}
		}

		private bool m_IsActive;
		/// <summary>
		/// Specify this tab and owner form is activate.
		/// </summary>
		public bool IsActive
		{
			get {return m_IsActive;}
			set {m_IsActive = value;}
		}

		private asMdiManager m_Parent;
		/// <summary>
		/// Property Parent (asMdiManager)
		/// </summary>
		public asMdiManager Parent
		{
			get {return m_Parent;}
			set {m_Parent = value;}
		}

		private BaseForm m_OwnerForm;
		/// <summary>
		/// Get owner form with this tab form.
		/// </summary>
		public BaseForm OwnerForm
		{
			get {return m_OwnerForm;}
		}

		private int m_FormId;
		/// <summary>
		/// Get or set form running.
		/// </summary>
		public int FormId
		{
			get {return m_FormId;}
			set {m_FormId = value;}
		}

		#endregion

		#region Drawing

		/// <summary>
		/// Measure string from name.
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		private Size MeasureString(string text)
		{
			Label lb = new Label();
			lb.Text = text;
			lb.AutoSize = true;
			int nWidth = lb.Width;
			lb.Dispose();

			return (new Size(nWidth + PADDING_SIZE, this.Height));
		}

		/// <summary>
		/// Hit test region locate in specify area.
		/// </summary>
		/// <param name="pt"></param>
		/// <returns></returns>
		public bool HitTest(Point pt)
		{
			Rectangle rect = new Rectangle(this.Location, this.Size);
			return (rect.Contains(pt));
		}

		/// <summary>
		/// Set Tab and OwnerForm Active.
		/// </summary>
		public void SetFormActive()
		{
			this.IsActive = true;
//			this.m_OwnerForm.Size = this.m_Parent.Content.Size;
			this.m_OwnerForm.Dock = DockStyle.Fill;
			this.m_OwnerForm.BringToFront();
		}

		/// <summary>
		/// Draw tab.
		/// </summary>
		/// <param name="g"></param>
		/// <param name="useGradient"></param>
		public void Draws(Graphics g, bool useGradient)
		{
			Rectangle rect = new Rectangle(this.Location, this.Size);
			Color bkColor = this.IsActive? this.m_BackColor: Color.Transparent;

			// Fill back color
			if (useGradient)
			{
				Color color1 = Color.FromArgb(70, bkColor);
				using (LinearGradientBrush lnBrush = new LinearGradientBrush(rect, color1, bkColor, 90f, true))
				{
					g.FillRectangle(lnBrush, rect);
				}
			}
			else
			{
				using (SolidBrush br = new SolidBrush(bkColor))
				{
					g.FillRectangle(br, rect);
				}
			}

			// Draw border tab, if active
			if (this.IsActive)
			{
				// Left
				g.DrawLine(new Pen(Color.White), rect.X, rect.Y, rect.X, rect.Height-1);
				// Top
				g.DrawLine(new Pen(Color.White), rect.X+1, rect.Y, rect.Right, rect.Y);
				// Right
				g.DrawLine(new Pen(Color.DimGray), rect.Right, rect.Top, rect.Right, rect.Height);
				// Bottom
				//g.DrawLine(new Pen(this.m_BackColor), rect.X, rect.Bottom, rect.X, rect.Right);
			}
			else
			{
				// Right edge only
				g.DrawLine(new Pen(Color.LightGray), rect.Right, rect.Top, rect.Right, rect.Height-3);
			}

			using (StringFormat sf = new StringFormat())
			{
				sf.Alignment = StringAlignment.Center;
				sf.LineAlignment = StringAlignment.Center;
				sf.Trimming = StringTrimming.EllipsisCharacter;

				g.DrawString(this.Text, SystemInformation.MenuFont, SystemBrushes.WindowText, rect, sf);
			}
		}



		#endregion
	}
}
